$N("Archetype.methodBuilders",{});

/**
 * @class MethodManager
 *
 * Manage Method Builders to apply to a Archetype.Component Definition
 */
MethodManager = Archetype.Class.create(
/** @scope MethodManager.prototype */
{
    /**
     * Set up a Method Manager
     */
    initialize: function() {
        if(typeof Archetype.methodBuilders == "undefined" || Archetype.methodBuilders.length == 0 )
            throw "No Method Builder has been defined in Archetype.methodBuilders";
    },
    /**
     * Return the available Method Builders for the component
     * ToDo: implement a way to configurate method builder per UMN
     *
     * @param {UniqueModuleName} umn
     * @return {Array of MethodBuilder}
     */
    getMethodBuilders: function(/*UniqueModuleName*/umn) {
        return Archetype.methodBuilders;
    },
    /**
     * Build methods of the component from the definition using the methodBuilders
     *
     * @param {Archetype.ComponentDefinition} definition /!\ this definition needs to have definition.setup.umn correctly set
     */
    buildMethods: function(/*Archetype.ComponentDefinition*/klass) {
        //keep only Methods
        //Logger.debug("Class :",klass);
        var methodNames=$H(klass._private._definition).keys().findAll(function(key){return typeof klass._private._definition[key] == "function"});
        //Logger.debug("Class Methods:",methodNames);
        var builders=this.getMethodBuilders(klass._private._definition.setup.url);

        // for each method, apply the method builders needed
        methodNames.each(function(methodName){
            //Logger.debug("methodName:", methodName);
            // get all builders
            var buildersArray = $H(builders).values();
            //Logger.debug("buildersArray:", buildersArray);
            // keep only relevant builders
            var relevantBuilders= buildersArray.findAll(function(b) {
                //Logger.debug("b.regexp:", b.regexp);
                return methodName.match(b.regexp)
            });
            //Logger.debug("relevantBuilders:", relevantBuilders);
            // apply the relevant builders to the current method
            relevantBuilders.each(function(builder) {
                //Logger.debug("builder:", builder, "methodName:", methodName);
                builder.build(klass, methodName)
            });
        });
    }
});

